package com.example.user

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.user.databinding.ActivityUpdatePwdBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/user/UpdatePwdActivity")
@AndroidEntryPoint
class UpdatePwdActivity : BaseActivity<ActivityUpdatePwdBinding,UserIntent,UserViewModel>() {
    override fun getLayout(): Int  = R.layout.activity_update_pwd

    override fun getVM(): Class<UserViewModel> = UserViewModel::class.java

    override fun initView() {
        binding.btnUpdatepwd.setOnClickListener {
            val newpwd = binding.etNewpwd.text.toString()
            val renewpwd = binding.etRenewpwd.text.toString()
            if(!RegexUtils.isMatch("^[\\w]{6,12}$",newpwd)){
                ToastUtils.showLong("新密码格式不正确，应该为长度为6-12位的数字字母下划线组合")
                return@setOnClickListener
            }
            if(newpwd!=renewpwd){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            viewModel.sendIntent(UserIntent.updatePwd(newpwd,SPUtils.getInstance().getInt("uid")))
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> -> {
                        ToastUtils.showLong("修改密码成功")
                        finish()
                    }
                    else->{}
                }
            }
        }
    }

}